class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *fast=head,*slow=head;
        if(fast==nullptr||fast->next==nullptr){
          return nullptr;
        }
        do{
          fast=fast->next->next;
          slow=slow->next;
          if(fast==nullptr||fast->next==nullptr){
             return nullptr;
          }}while(slow!=fast);
        ListNode *h=head;
        while(h!=slow){
          h=h->next;
          slow=slow->next;
        }
        return h;
    }
};
